<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptions">
      <template #form="{ form }">
        <el-input v-model="form.account" placeholder="使用邮箱" class="w-40" />
        <VCascader v-model="form.purchase_user_id" placeholder="采购人员" multiple collapse-tags filterable clearable style="min-width: 160px" />
        <VGroup>
          <div class="v-group-item">更新时间</div>
          <VDate v-model="form.dateRange.value" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <VButton auth="/purchase/settings/email/add" type="primary" @click="handleRow('add')"><i class="dk-iconfont icon-Plus" />添加
        </VButton>
        <VButton auth="/purchase/settings/email/template" @click="handleBatch('template')">邮件模板设置</VButton>
        <VExport auth="/purchase/settings/email/export" name="email" mainKey="e_id" :tableRef="tableRef" task class="ml-auto" />
      </template>
    </VTable>
    <Add ref="addRef" :options="options" @success="query" />
    <Template ref="templateRef" :options="options" @success="query" />
    <VLog ref="logRef" />
  </VPage>
</template>
  
<script setup lang="jsx" name="Account">
import api from './api'
import Add from './components/Add'
import Template from './components/Template'
import { getOption } from '@/api/public'
import { ElMessage, ElMessageBox, ElSwitch } from 'element-plus'

const baseName = 'email'
const tableRef = ref()
const logRef = ref()
const loadings = reactive({})
// 查询
const query = () => {
  tableRef?.value.query()
}

// 获取options
const options = ref({})
Promise.all([
  getOption({ type: baseName }).then(res => res.data),
  Promise.resolve({ test: [{ label: '没有数据', value: 1 }] })
]).then(res => {
  options.value = res.reduce((acc, cur) => (((acc = { ...acc, ...cur }), acc)), {})
})

// 批量操作
const templateRef = ref()
const handleBatch = (name) => {
  const selection = tableRef.value.$table.getCheckboxRecords()
  if (!selection.length) {
    ElMessage.warning('请先选择行')
    return
  }
  const e_ids = selection.map(d => d.e_id)
  const showLoading = () => loadings[name] = true
  const closeLoading = () => loadings[name] = false
  const handle = {
    template: () => {
      templateRef?.value.open(e_ids)
    },
  }
  handle[name]()
}

// 单行操作
const addRef = ref()
const handleRow = (name, row, rowIndex) => {
  const { e_id } = row || {}
  const showLoading = () => {
    loadings[name + rowIndex] = true
  }
  const closeLoading = () => {
    loadings[name + rowIndex] = false
  }
  const handle = {
    add: () => {
      addRef?.value.open()
    },
    edit: () => {
      addRef?.value.open(e_id)
    },
    log: () => {
      logRef?.value.open({ log_type: baseName, e_id })
    },
  }
  handle[name]()
}

// 列表配置
const tableOptions = reactive({
  showHeaderOverflow: false,
  showOverflow: false,
  formConfig: {
    data: {
      dateRange: { type: 'update_time', value: [], range: true },
    }
  },
  columns: [
    { type: 'checkbox', width: 40, fixed: 'left' },
    { title: '采购人员', field: 'purchase_user', slots: {
      default: ({row}) => <VText value={row.purchase_user} />
    } },
    { title: '使用邮箱', field: 'account', minWidth: 140, slots: {
      default: ({row}) => <VText value={row.account} />
    } },
    { title: '添加人', field: 'create_user', slots: {
      default: ({row}) => <VText value={row.create_user} />
    } },
    { title: '更新时间', field: 'update_time', slots: {
      default: ({row}) => <VText value={row.update_time} />
    } },
    {
      title: '状态', field: 'status_name', width: 120, slots: {
        default: ({ row }) => <VStatus value={row.status_name === '启用'} />
      }
    },
    {
      title: '操作', width: 120, fixed: 'right', slots: {
        default: ({ row, rowIndex }) => {
          return <div class='nm flex flex-wrap gap-x-2 gap-y-1'>
            <VButton auth="/purchase/settings/email/edit" link type="primary" onClick={() => handleRow('edit', row)}>编辑</VButton>
            <VButton auth="/purchase/settings/email/log" link type="primary" onClick={() => handleRow('log', row)}>日志</VButton>
          </div>
        }
      }
    },
  ],
  proxyConfig: {
    autoLoad: false,
    ajax: {
      query: ({ page, form }) => api.query({ ...form, ...page })
    }
  }
})
</script>
  